/*
MIT Licence allows free use in all software (including GPL and commercial)

The licence text below is the boilerplate "MIT Licence" used from:
http://www.opensource.org/licenses/mit-license.php

Copyright (c) 2012, An Pham

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#ifndef H_MFLINKTOOLINIFILE
#define H_MFLINKTOOLINIFILE

#include <string>
#include <vector>
#include <iostream>
#include <fstream>
#include <algorithm>
#include <functional>
#include "MFLinkToolUtil.h"

using namespace std;

class IniFile
{
public:
	enum ElementKind
	{
		Comment,
		Key,
		Section
    };

    struct Element
    {
        string Section, Key, Value;
		ElementKind Kind;
    };
protected:
    struct SectionIs : std::unary_function<Element, bool>
    {
        string Section;

        SectionIs(const string& ASection): Section(ASection)
		{
		}

        bool operator()(const Element& AEle) const
        {
            return ((IniFile::Section == AEle.Kind) && (0 == strCompare(AEle.Section, Section)));
        }
    };

    struct KeyIs : std::unary_function<Element, bool>
    {
        string Section, Key;

        KeyIs(const string& ASection, const string& AKey): Section(ASection), Key(AKey)
		{
		}

        bool operator()(const Element& AEle) const
        {
            return ((IniFile::Key == AEle.Kind) && (0 == strCompare(AEle.Section, Section)) && (0 == strCompare(AEle.Key, Key)));
        }
    };
protected:
	void loadLine(string& AStr, string& ALastSection);
public:
	vector<Element> Content;    
	string FileName;
	bool Changed;
public:
	IniFile();
	virtual ~IniFile();

    void addKey(const string& ASection, const string& AKey);
    void addSection(const string& ASection);
    bool deleteKey(const string& ASection, const string& AKey);
    bool deleteSection(const string& ASection);
    bool existsKey(const string& ASection, const string& AKey);
    bool existsSection(const string& ASection);
    vector<Element> getKey(const string& ASection, const string& AKey);
    vector<Element> getSection(const string& ASection);
    vector<string> getSectionNames();
    string getValue(const string& ASection, const string& AKey, const string ADefault = "");
	void merge(const IniFile& ASource);
    bool renameSection(const string& AOldSection, const string& ANewSection);
    void setValue(const string& ASection, const string& AKey, const string& AValue);

    void loadFromFile(const string& AFileName);
	void loadFromStream(ifstream& AFileStream);
	void loadFromStream(stringstream& AFileStream);
	void loadFromString(const string& AStr);
    void save();
    void saveToFile(const string& AFileName);
    void saveToStream(ofstream& AFileStream);
};

#endif /* H_MFLINKTOOLINIFILE */